package com.java110.api.listener.fee;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.java110.api.listener.AbstractServiceApiPlusListener;
import com.java110.core.annotation.Java110Listener;
import com.java110.core.context.DataFlowContext;
import com.java110.core.event.service.api.ServiceDataFlowEvent;
import com.java110.dto.RoomDto;
import com.java110.intf.community.IRoomInnerServiceSMO;
import com.java110.intf.fee.IPayFeeConfigDiscountInnerServiceSMO;
import com.java110.utils.constant.lm.service.code.WeEnum;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.List;
import java.util.Map;

@Java110Listener("wePriceListener")
public class WePriceListener extends AbstractServiceApiPlusListener {
    @Autowired
    private IPayFeeConfigDiscountInnerServiceSMO payFeeConfigDiscountInnerServiceSMOImpl;
    @Autowired
    private IRoomInnerServiceSMO roomInnerServiceSMOImpl;

    @Override
    protected void validate(ServiceDataFlowEvent event, JSONObject reqJson) {

    }

    @Override
    protected void doSoService(ServiceDataFlowEvent event, DataFlowContext context, JSONObject reqJson) {
        RoomDto roomDto = BeanConvertUtil.covertBean(reqJson, RoomDto.class);
        List<RoomDto> rooms = roomInnerServiceSMOImpl.queryRoomsByOwner(roomDto);
        List<Map<String, Object>> prices = payFeeConfigDiscountInnerServiceSMOImpl.findWePrice(roomDto.getCommunityId());
        // 住宅
        Double wMoney = 0D;
        Double eMoney = 0D;
        // 商铺
        Double eMoney01 = 0d;
        Double wMoney01 = 0d;

        for (Map<String, Object> price : prices) {
            String type = price.get("type").toString();
            if(type.equals("88880001001601")){
                eMoney01 = (Double) price.get("price");
            } else if(type.equals("88880001001501")){
                wMoney01 = (Double) price.get("price");
            } else if(type.equals("888800010016")){
                eMoney = (Double) price.get("price");
            } else if(type.equals("888800010015")){
                wMoney = (Double) price.get("price");
            }
        }
        List<Map<String,Object>> list = Lists.newArrayList();
        for (RoomDto room : rooms) {
            Map<String,Object> map = Maps.newHashMap();
            map.put("value",room.getRoomId());
            // 商铺
            if(room.getRoomType().equals("2020602")){
                map.put("text",room.getFloorNum() + "栋" + room.getRoomNum() + "铺（商铺）");
                map.put("water",wMoney01);
                map.put("type","2020602");
                map.put("electricity",eMoney01);
                map.put("wNum",room.getWaterNum());
                map.put("eNum",room.getElectricityNum());
            } else if(room.getRoomType().equals("1010301")){
                map.put("text",room.getUnitNum() + "单元" + room.getRoomNum() + "栋" + room.getRoomNum() + "房（住宅）");
                map.put("water",wMoney);
                map.put("electricity",eMoney);
                map.put("type","1010301");
                map.put("wNum",room.getWaterNum());
                map.put("eNum",room.getElectricityNum());
            }
            list.add(map);
        }
        ResultVo vo = new ResultVo(list);
        ResponseEntity<String> responseEntity = new ResponseEntity(vo.toString(), HttpStatus.OK);
        context.setResponseEntity(responseEntity);
    }

    @Override
    public String getServiceCode() {
        return WeEnum.we_price.getCode();
    }

    @Override
    public HttpMethod getHttpMethod() {
        return HttpMethod.GET;
    }
}
